#include "BH1750.h"

static struct bflb_device_s *i2c;
static struct bflb_i2c_msg_s msgs[2];
/**************************Local Function**************************/
void IIC0_Init(void)
{
	struct bflb_device_s* gpio;
	
	gpio = bflb_device_get_by_name("gpio");
	/* I2C0_SCL */
	bflb_gpio_init(gpio, GPIO_PIN_0, GPIO_FUNC_I2C0 | GPIO_ALTERNATE | GPIO_PULLUP | GPIO_SMT_EN | GPIO_DRV_1);
	/* I2C0_SDA */
	bflb_gpio_init(gpio, GPIO_PIN_1, GPIO_FUNC_I2C0 | GPIO_ALTERNATE | GPIO_PULLUP | GPIO_SMT_EN | GPIO_DRV_1);
    
	/* IIC初始化，速度为400K */
	i2c = bflb_device_get_by_name("i2c0");
	bflb_i2c_init(i2c, 400000);
	
    //printf("IIC0 Init over\r\n");
}
void IIC_SenCmd(uint8_t _Cmd, uint8_t *Sendbuf, uint16_t _length)
{
	if (BH1750_ADDR)
	{
		msgs[0].addr = BH1750_ADDR_H;
	}
	else
	{
		msgs[0].addr = BH1750_ADDR_L;
	}
    msgs[0].flags = I2C_M_NOSTOP;
    msgs[0].buffer = &_Cmd;
    msgs[0].length = 1;

	if (_length != 0)
	{
		/* 有数据的发送 */
		if (BH1750_ADDR)
		{
			msgs[0].addr = BH1750_ADDR_H;
		}
		else
		{
			msgs[0].addr = BH1750_ADDR_L;
		}
     	msgs[1].flags = 0;
     	msgs[1].buffer = Sendbuf;
     	msgs[1].length = _length;
     	bflb_i2c_transfer(i2c, msgs, 2);
	}
	else
	{
		/* 只发一个字节的数据 */
		msgs[0].flags = 0;
		bflb_i2c_transfer(i2c, msgs, 1);
	}
}
void IIC_RecData(uint8_t *_Recbuf, uint16_t _length)
{
   	if (BH1750_ADDR)
	{
		msgs[0].addr = BH1750_ADDR_H;
	}
	else
	{
		msgs[0].addr = BH1750_ADDR_L;
	}
    msgs[0].flags = I2C_M_READ;
    msgs[0].buffer = _Recbuf;
    msgs[0].length = _length;
    bflb_i2c_transfer(i2c, msgs, 1);
}

void BH1750_SetPowerSta(BH1750PowerSta _powerSta)
{
	if (BH1750_POWER_STA_ON == _powerSta)
	{
		IIC_SenCmd(BH1750_POWAER_ON_CMD, 0, 0);
		printf("Chip Power ON CMD send over\r\n");
	}
	else
	{
		IIC_SenCmd(BH1750_POWAER_OFF_CMD, 0, 0);
		printf("Chip Power OFF CMD send over\r\n");
	}
    
}

void BH1750_Reset(void)
{
	IIC_SenCmd(BH1750_RESET_CMD, 0, 0);
	
    //printf("Chip Reset CMD send over\r\n");
}

void BH1750_SendTestMode(uint8_t _TestMode)
{
	IIC_SenCmd(_TestMode, 0, 0);

	//printf("Chip Test Mode CMD send over\r\n");
}

/**************************Global Function**************************/
void BH1750_Init()
{
	/* IIC初始化 */
	IIC0_Init();

	/* 芯片通电 */
	BH1750_SetPowerSta(BH1750_POWER_STA_ON);

}
// int32_t TestModePoll = 0;
// BH1750ReadRes BH1750_TestModePoll(float *_LuxVal)
// {
// 	uint8_t RecData[3] = {0};
// 	uint16_t Temp = 0;

// 	if (TestModePoll == 0)
// 	{
// 		BH1750_SendTestMode(BH1750_TEST_MODE_CON_H_CMD);
// 		printf("Con H Test Mode\r\n");
// 	}
// 	else if (TestModePoll == 1)
// 	{
// 		BH1750_SendTestMode(BH1750_TEST_MODE_CON_H_2_CMD);
// 		printf("Con H2 Test Mode\r\n");
// 	}
// 	else if (TestModePoll == 2)
// 	{
// 		BH1750_SendTestMode(BH1750_TEST_MODE_CON_L_CMD);
// 		printf("Con L Test Mode\r\n");
// 	}
// 		else if (TestModePoll == 3)
// 	{
// 		BH1750_SendTestMode(BH1750_TEST_MODE_ONCE_H_CMD);
// 		printf("Once H Test Mode\r\n");
// 	}
// 		else if (TestModePoll == 4)
// 	{
// 		BH1750_SendTestMode(BH1750_TEST_MODE_ONCE_H_2_CMD);
// 		printf("Once H2 Test Mode\r\n");
// 	}
// 		else if (TestModePoll == 5)
// 	{
// 		BH1750_SendTestMode(BH1750_TEST_MODE_ONCE_L_CMD);
// 		printf("Once L Test Mode\r\n");
// 	}

// 	if ((BH1750_TEST_MODE == BH1750_TEST_MODE_CON_L_CMD) || (BH1750_TEST_MODE == BH1750_TEST_MODE_ONCE_L_CMD))
// 	{
// 		bflb_mtimer_delay_ms(BH1750_TEST_DELAY_24MS);
// 	}
// 	else
// 	{
// 		bflb_mtimer_delay_ms(BH1750_TEST_DELAY_180MS);
// 	}

// 	IIC_RecData(RecData, 2);

// 	printf("1 is %x,  2 is %x\r\n",RecData[0],RecData[1]);
// 	//if ((BH1750_TEST_MODE == BH1750_TEST_MODE_CON_H_CMD) || (BH1750_TEST_MODE == BH1750_TEST_MODE_ONCE_H_CMD))
// 	if ((TestModePoll == 0) || (TestModePoll == 3))
// 	{
// 		Temp = (RecData[0] << 8) | RecData[1];
// 		*_LuxVal = (float)Temp / 1.2;
// 	}
// 	//else if ((BH1750_TEST_MODE == BH1750_TEST_MODE_CON_H_2_CMD) || (BH1750_TEST_MODE == BH1750_TEST_MODE_ONCE_H_2_CMD))
// 	else if((TestModePoll == 1) || (TestModePoll == 4))
// 	{
// 		if ((RecData[1] & 0x01) == 0x01)
// 		{
// 			Temp = (RecData[0] << 7) | (RecData[1] >> 1);
// 			*_LuxVal = ((float)Temp + 0.5) / 1.2;
// 		}
// 		else
// 		{
// 			Temp = (RecData[0] << 7) | (RecData[1] >> 1);
// 			*_LuxVal = (float)Temp / 1.2;
// 		}
// 	}
// 	else
// 	{
// 		Temp = (RecData[0] << 8) | RecData[1];
// 		*_LuxVal = (float)Temp / 1.2;
// 	}

// 	TestModePoll++;
// 	if (TestModePoll == 6)
// 	{
// 		TestModePoll = 0;
// 	}

// 	return BH1750_READ_OK;
// }



BH1750ReadRes BH1750_ReadLightIntensity(float *_LuxVal)
{
	uint8_t RecData[3] = {0};
	uint16_t Temp = 0;

	if ((BH1750_TEST_MODE == BH1750_TEST_MODE_ONCE_H_CMD) || (BH1750_TEST_MODE == BH1750_TEST_MODE_ONCE_H_2_CMD) || (BH1750_TEST_MODE == BH1750_TEST_MODE_ONCE_L_CMD))
	{
		/* 芯片通电 */
		BH1750_SetPowerSta(BH1750_POWER_STA_ON);
	}

	BH1750_SendTestMode(BH1750_TEST_MODE);
	
	if ((BH1750_TEST_MODE == BH1750_TEST_MODE_CON_L_CMD) || (BH1750_TEST_MODE == BH1750_TEST_MODE_ONCE_L_CMD))
	{
		bflb_mtimer_delay_ms(BH1750_TEST_DELAY_24MS);
	}
	else
	{
		bflb_mtimer_delay_ms(BH1750_TEST_DELAY_180MS);
	}

	IIC_RecData(RecData, 2);

	if ((BH1750_TEST_MODE == BH1750_TEST_MODE_CON_H_2_CMD) || (BH1750_TEST_MODE == BH1750_TEST_MODE_ONCE_H_2_CMD))
	{
		if ((RecData[1] & 0x01) == 0x01)
		{
			Temp = (RecData[0] << 7) | (RecData[1] >> 1);
			*_LuxVal = ((float)Temp + 0.5) / 1.2;
		}
		else
		{
			Temp = (RecData[0] << 7) | (RecData[1] >> 1);
			*_LuxVal = (float)Temp / 1.2;
		}
	}
	else
	{
		Temp = (RecData[0] << 8) | RecData[1];
		*_LuxVal = (float)Temp / 1.2;
	}

	return BH1750_READ_OK;
}

